package day0428.com.ch16threeLayer.view;

import day0428.com.ch16threeLayer.entity.User;
import day0428.com.ch16threeLayer.service.IUserService;
import day0428.com.ch16threeLayer.service.Impl.UserService;
import day0428.com.ch16threeLayer.service.IUserService;

import java.util.Scanner;

/**
 * 作者：wangmaokang
 * 日期: 2021/4/28 10:13
 * 描述:
 */
public class UserView {

    public static void main(String[] args) {

        IUserService userService= (IUserService) new UserService();
        Scanner scanner=new Scanner(System.in);

        boolean isLogin=false;


        while (true){

            if (isLogin){
                isLogin= selectFun(scanner,isLogin);
            }else {
                System.out.println("请输入您的用户名和密码");
                String name=scanner.nextLine();
                String passWord=scanner.nextLine();
                User user=userService.login(name,passWord);
                if (user==null){
                    System.out.println("用户名密码错误");
                }else {
                    isLogin=true;
                    System.out.println("登录成功");
                    isLogin= selectFun(scanner,isLogin);
                }
            }
        }

    }

    static boolean selectFun( Scanner scanner,boolean isLogin){
        System.out.println("====================================");
        System.out.println("请选择您要的操作");
        System.out.println("1.id查询用户");
        System.out.println("2.获取所有用户");
        System.out.println("3.添加用户");
        System.out.println("4.删除用户");
        System.out.println("5.退出登录");

        String num=scanner.nextLine();
        if ("1".equals(num)){
            System.out.println("请输入您要查询的用户id");
            String id=scanner.nextLine();
            getUserById(Integer.parseInt(id));
        }else if ("2".equals(num)){
            System.out.println("获取所有用户");
        }else if ("3".equals(num)){
            System.out.println("添加用户");
        }else if ("4".equals(num)){
            System.out.println("删除用户");
        } else if ("5".equals(num)){
            System.out.println("用户退出登录");
            isLogin=false;
        }
          return isLogin;
    }

   static void getUserById(int id){
       IUserService userService= (IUserService) new UserService();
       try {
          User user= userService.getUserById(id);
           System.out.println(user);
       } catch (Exception e) {
           e.printStackTrace();
       }
   };

    static void getAllUsers(){};

    static void addUser(User user){};

    static void deleteUserById(int id){};
}
